package com.gruppsex.trainingdairy;


import android.app.Activity;
import org.achartengine.ChartFactory;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class StatisticActivity extends Activity implements
		OnItemSelectedListener {

	DataBaseHelper db = new DataBaseHelper(this);
	private String[] date;
	double[] calories;
	private int[] points;
	private Spinner spinner;
	private ArrayAdapter<String> adapter;
	private String[] activitys = new String[] {};
	
	private RadioButton RBcalories = null, RBreps = null, RBsets = null, RBkm = null,
			RBtime = null, RBweight = null;
	private RadioGroup radio_group;
	private String str;
	private String activitySpinner;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.statistic);
		RBweight=(RadioButton)findViewById(R.id.RBweight);
		RBweight.setEnabled(false);
		RBtime=(RadioButton)findViewById(R.id.RBtime);
		RBtime.setEnabled(false);
		spinnerWithActivities();

	}

	public void spinnerWithActivities() {
		spinner = (Spinner) findViewById(R.id.spinnerActivities);
		db.openDataBase();
		activitys = db.getActivity();
		db.close();
		adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, activitys);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		spinner.setAdapter(adapter);
		spinner.setOnItemSelectedListener(this);
	}

	public void onItemSelected(AdapterView<?> parent, View v, int position,
			long id) {		 
		activitySpinner=activitys[position];
		
	}

	public void onNothingSelected(AdapterView<?> parent) {
		// selection.setText("");
	}
    private void openChart(int [] punkter, double [] values, String [] datum, String text){
        // Creating an  XYSeries for Expense
        XYSeries expenseSeries = new XYSeries(text + " over time");
        // Adding data to Income and Expense Series
        for(int i=0;i<punkter.length;i++){
            
            expenseSeries.add(punkter[i],values[i]);
        }
 
        // Creating a dataset to hold each series
        XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
        // Adding Expense Series to dataset
        dataset.addSeries(expenseSeries);
        // Creating XYSeriesRenderer to customize expenseSeries
        XYSeriesRenderer expenseRenderer = new XYSeriesRenderer();
        expenseRenderer.setColor(Color.YELLOW);
        expenseRenderer.setPointStyle(PointStyle.CIRCLE);
        expenseRenderer.setFillPoints(true);
        expenseRenderer.setLineWidth(2);
        expenseRenderer.setDisplayChartValues(true);
 
        // Creating a XYMultipleSeriesRenderer to customize the whole chart
        XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();
        multiRenderer.setXLabels(0);
        multiRenderer.setChartTitle(text + " over time in date chart");
        multiRenderer.setXTitle("Date");
        multiRenderer.setYTitle(text);
        multiRenderer.setZoomButtonsVisible(true);
        for(int i=0;i<punkter.length;i++){
            multiRenderer.addXTextLabel(i+1, datum[i]);
        }
 
        // Adding incomeRenderer and expenseRenderer to multipleRenderer
        // Note: The order of adding dataseries to dataset and renderers to multipleRenderer
        // should be same
        multiRenderer.addSeriesRenderer(expenseRenderer);
 
        // Creating an intent to plot line chart using dataset and multipleRenderer
        Intent intent = ChartFactory.getLineChartIntent(getBaseContext(), dataset, multiRenderer);
 
        // Start Activity
        startActivity(intent);
    }
        
        
        public void createArray(String activity,String values){
        	int x = 1;
        	
        	date = db.getFind("date", activity);
        	        	        	
        	calories = db.getFinds(values, activity);
        	//String[] date = db.getDate("Running");
        	points =  new int[db.countPass(activity)];
        	
        	for (int i = 0; i < points.length; i++){
        		points[i]= x;
        		x++;
        	}
        	openChart(points, calories, date, str);
        }
	
	public void onRadioButtonClicked(View view) {
		
		boolean checked = ((RadioButton) view).isChecked();
		
		switch (view.getId()) {
		case R.id.RBcalories:
			
			str = "calories";
			break;
		case R.id.RBkm:
			str = "km";
			break;
		case R.id.RBreps:
			str = "reps";
			break;
		case R.id.RBsets:
			str = "sets";
			break;
		case R.id.RBtime:
			str = "Time";
			break;
		case R.id.RBweight:
			str = "weight";
			break;

		}
		Toast.makeText(this, str, Toast.LENGTH_SHORT).show();

	}
	public void onClickViewGraph(View v){
		switch (v.getId()) {
		case R.id.buttonViewGraph:
			createArray(activitySpinner, str);
			break;
	}
	}
}
